function [Yhat intLim]=ecogWienerMIMORegressionPredict(ecog,W,intervalLength,intervalOffset, normParam)
%[Yhat]=ecogWienerMIMORegressionPredict(ecog,Y,W,intervalLength,intervalOffset) Apply MIMO Wiener regression to predict Yhat
%
% Purpose: Apply Multiple Input Multiple Output (MIMO) Wiener regression 
% to predict Yhat from X accoring to Yhat=X*W, i.e. predicts Y 
% (the movement or stimulus feature)from X (the ecog data).
%
% Yhat is predicted from multiple shifted version of X because W extends in
% time and establishes a certain temporal relation between X and Y that can 
% be interpreted in terms of causality (see below). Heres X causal on Y means
% that effects in X have an influence on what happens later in time in Y. 
% 
% INPUT:
% ecog:     An ecog structure with fields:
%           data
%           sampDur
% W:        The matrix W as returned by ecogWienerMIMORegressionTrain
%           The first entry might be an offset and will be involved in the reconstruction(CR 4.6.2010).
% intervalLength:
%           The length of the intervall of the regression filter 
%           (in units of ecog.sampDur). The number of estimated regression parameters 
%           per external variable is intervalLengthInSamples*numberOfChannelsInX 
% intervalOffset:
%           Time offset between the brain data and external variables:
%           Four cases can be distinguished:
% 
%           intervalOffset<=-intervalLength: 
% returns a regression marix W with purely  
% causal effects of X on Y. If both absolute values are equal returns the  
% W with the shortest lag with purely causal effects of X on Y. Decreasing  
% the offset further (its a negative value!) produces X on Y causal Ws with  
% longer lags
% 
%           0>=intervalOffset>-intervalLength: 
% returns a matrix W producing mixed X 
% and Y on X causal effects. 
%
%           intervalOffset>=1: 
%produces the filter with the shortest lag with purely 
% causal effects of Y on X. Further increasing the offset (its a positive 
% value!) produces Y on X causal Ws with longer lags.
%
%           intervalOffset=0 and intervalLength=1
%           standard regression
%           normParam (optional) - struct with params for normalization (see 2nd
%           output argument from ecogWienerMIMIRegressionTrain)
% OUTPUT:
% Yhat:     The predicted Y-values.  
%           Due to the filter length some Yhat-values at the end will be
%           set to zero. Moreover, with negative offsets some Yhat-values at
%           the begin will be zero. This is because the Wiener regression 
%           working backwards in time cannot predit all Yhats at the begin. 
%           NEED A WORKAROUND FOR THIS: MAYBE PREPENDING X with zeros
% intLim:   interval limits for evaluation of Y and Yhat 
%
% Requirements: 

% 091129 JR wrote it based on Brian's code
% 100606 CR improved chopping (use most possible timesteps and remove bug
% for case 1)
% 111111 CR: create sub functions ecogPrepareRegression,
% ecogTrainRegression, ecogPredictRegression


%% Input check
if nargin <5,
    normParam = struct;
end

intervalLengthSamp=round(intervalLength/ecog.sampDur); % ms to samples
intervalOffsetSamp=round(intervalOffset/ecog.sampDur);
% get Input data
[X]=ecogPrepareRegression(ecog.data,[],intervalLengthSamp,intervalOffsetSamp);

%normParam.lengthY=size(ecog.data,2);
[Yhat intLim]= ecogPredictRegression(X,W, normParam,intervalLengthSamp,intervalOffsetSamp);
